#!/bin/bash
Fail()
{
    echo "FATAL(demo/c/demotest): $1"
    exit 1
}

TestDemo()
{
    echo "Testing C demo: $1"
    bin/$* > test/$1${SUFFIX}.txt || Fail "Error running demo: $1"
    diff {correct,test}/$1${SUFFIX}.txt || Fail "Incorrect output: $1"
}

rm -f test/*.txt
mkdir -p test
[[ "$1" == "nobuild" ]] || ./build || Fail "Error building example programs."

SUFFIX=
TestDemo solar_time +38.88 -77.03 2023-02-12T17:00:00Z
SUFFIX=_nz TestDemo camera -41.17 175.5 2023-03-24T06:30:00Z
SUFFIX=_fl TestDemo camera +29 -81 2023-03-25T00:00:00Z
SUFFIX=_ca TestDemo camera 51.05 -114.07 2023-03-24T02:07:31.000Z
TestDemo moonphase 2019-06-15T09:15:32.987Z
TestDemo positions +45.6 -90.7 2018-11-30T17:55:07.234Z
TestDemo riseset 45 -90 2019-06-14T21:45:25Z
TestDemo seasons 2020
TestDemo culminate +30 -90 2015-02-28T00:00:00Z
TestDemo horizon +25.5 -85.3 2016-12-25T12:30:45Z
TestDemo lunar_eclipse 1988-01-01T00:00:00Z
TestDemo galactic 38.92056 -77.0658 22.793498 197.070510 2025-04-06T00:00:00Z
TestDemo triangulate 48.16042 24.49986 2019 18 7 48.27305 24.36401 662 83 12
TestDemo ecliptic_vector 2022-05-20T23:58:19Z

echo "Testing C demo: gravity"
for latitude in {0..90}; do
    bin/gravity ${latitude} 0 >> test/gravity.txt || Fail "Error running gravity.c."
done
diff {correct,test}/gravity.txt || Fail "Error comparing gravity.c output."

cd raytrace || Fail "error changing to raytrace directory"
./build || Fail "build raytracer"
./run || Fail "run raytracer"
cd ..

./run_worldmap || Fail "error in run_worldmap"

echo "PASS: C demos"
exit 0
